package com.xin.data.simbapesa.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;

import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.base.BaseBindingAdapter1;
import com.xin.data.simbapesa.databinding.SimbaPItem1Binding;
import com.xin.data.simbapesa.databinding.SimbaPItem2Binding;
import com.xin.data.simbapesa.databinding.SimbaPItem3Binding;
import com.xin.data.simbapesa.obj.SimbaInfoData;
import com.xin.data.simbapesa.obj.SimbaValueInfo;
import com.xin.data.simbapesa.utils.LogUtil;
import com.xin.data.simbapesa.utils.Tools;
import com.xin.data.simbapesa.utils.ViewUtils;
import com.xin.data.simbapesa.view.SimbaEditText;
import com.xin.data.simbapesa.view.SimbaTextView;

import java.util.HashMap;
import java.util.List;

public class PersonalAdater extends BaseBindingAdapter1<SimbaInfoData, ViewBinding> {


    String stepTag;

    ClickSelectInterface onClickSelectInterface;
    HashMap<String, SimbaEditText> inputList = new HashMap<>();

    String salaryFrequencyValue;

    boolean isError;

    public PersonalAdater(List<SimbaInfoData> dataList) {
        super(dataList);
    }

    @SuppressLint({"NotifyDataSetChanged", "SetTextI18n"})
    @Override
    protected void bindData(ViewBinding binding, SimbaInfoData item, int position) {
        if (binding instanceof SimbaPItem1Binding) {
            SimbaPItem1Binding binding1 = (SimbaPItem1Binding) binding;
            binding1.tvInput.setType(PU.P28 + stepTag + "_" + item.getParamName().toUpperCase() + "_INPUT");

            binding1.tvTitle.setSelected(false);
            binding1.tvInput.setSelected(false);

            if (!TextUtils.isEmpty(item.getParamValueTitle())) {
                binding1.tvInput.setText(item.getParamValueTitle());
            } else {
                binding1.tvInput.setText("");
                if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                    binding1.tvTitle.setSelected(isError);
                    binding1.tvInput.setSelected(isError);
                }
            }

            binding1.tvInput.clearFocus();

            binding1.tvTitle.setText(item.getTitle());
            binding1.tvInput.setHint(item.getHint());

            if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                binding1.tvMust.setVisibility(View.VISIBLE);
            } else {
                binding1.tvMust.setVisibility(View.INVISIBLE);
            }


            binding1.tvInput.setTag(position);
            binding1.tvInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    int i = (int) binding1.tvInput.getTag();
                    if (binding1.tvInput.isProgrammaticChange() && position == i) {
                        String data = String.valueOf(s);
                        if (!TextUtils.isEmpty(data)) {
                            item.setParamValue(data);
                            item.setParamValueTitle(data);
                            if (isError) {
                                isError = false;
                            }
                        } else {
                            item.setParamValue("");
                            item.setParamValueTitle("");
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

//            if (!TextUtils.isEmpty(item.getParamName()) && item.getParamName().equals("monthlyIncome")) {
//                binding1.tvInput.setInputType(InputType.TYPE_CLASS_NUMBER);
//            } else {
//                binding1.tvInput.setInputType(InputType.TYPE_CLASS_TEXT);
//            }

            binding1.getRoot().setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));

            binding1.tvTitle.setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));

            inputList.put(String.valueOf(position), binding1.tvInput);

        } else if (binding instanceof SimbaPItem2Binding) {
            SimbaPItem2Binding binding1 = (SimbaPItem2Binding) binding;

            binding1.tvInput.setType(PU.P28 + stepTag + "_" + item.getParamName().toUpperCase() + "_INPUT");

            binding1.tvTitle.setSelected(false);
            binding1.tvInputLayout.setSelected(false);

            if (!TextUtils.isEmpty(item.getParamValueTitle())) {
                binding1.tvInput.setText(item.getParamValueTitle());
            } else {
                binding1.tvInput.setText("");
                if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                    binding1.tvTitle.setSelected(isError);
                    binding1.tvInputLayout.setSelected(isError);
                }
            }

            binding1.tvInput.clearFocus();

            binding1.tvTitle.setText(Html.fromHtml(item.getTitle()));
            binding1.tvInput.setHint(item.getHint());

            if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                binding1.tvMust.setVisibility(View.VISIBLE);
            } else {
                binding1.tvMust.setVisibility(View.INVISIBLE);
            }

            if (binding1.tvInput.getText() != null) {
                binding1.tvNum.setText(binding1.tvInput.getText().length() + "/21");
            } else {
                binding1.tvNum.setText("0/21");
            }

            LogUtil.e("me", "<-tvCode->" + item.getBankCode());


            binding1.tvCode.setText(item.getBankCode());
            binding1.tvInput.setTag(position);
            binding1.tvInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    int i = (int) binding1.tvInput.getTag();
                    if (binding1.tvInput.isProgrammaticChange() && position == i) {
                        String data = String.valueOf(s);
                        if (!TextUtils.isEmpty(data)) {
                            item.setParamValue(data);
                            item.setParamValueTitle(data);
                            binding1.tvDelete.setVisibility(View.VISIBLE);

                            if (isError) {
                                isError = false;
                            }

                        } else {
                            item.setParamValue("");
                            item.setParamValueTitle("");
                            binding1.tvDelete.setVisibility(View.GONE);
                        }
                        binding1.tvNum.setText(binding1.tvInput.getText().length() + "/21");
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            binding1.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewUtils.viewScale(v);
                    binding1.tvInput.setText("");
                    item.setParamValue("");
                    item.setParamValueTitle("");
                    binding1.tvDelete.setVisibility(View.GONE);
                    binding1.tvNum.setText(binding1.tvInput.getText().length() + "/21");
                }
            });

            binding1.tvTitle.setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));

            binding1.getRoot().setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));

            inputList.put(String.valueOf(position), binding1.tvInput);


        } else if (binding instanceof SimbaPItem3Binding) {

            SimbaPItem3Binding binding1 = (SimbaPItem3Binding) binding;
            binding1.tvInput.setType(PU.P28 + stepTag + "_" + item.getParamName().toUpperCase() + "_CLICK");

            binding1.tvTitle.setText(item.getTitle());
            binding1.tvInput.setHint(item.getHint());

            if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                binding1.tvMust.setVisibility(View.VISIBLE);
            } else {
                binding1.tvMust.setVisibility(View.INVISIBLE);
            }

            binding1.tvTitle.setSelected(false);
            binding1.tvInput.setSelected(false);

            if (!TextUtils.isEmpty(item.getParamValueTitle())) {
                binding1.tvInput.setText(item.getParamValueTitle());
            } else {
                binding1.tvInput.setText("");
                if (!TextUtils.isEmpty(item.getIsMust()) && item.getIsMust().equals("YES")) {
                    binding1.tvTitle.setSelected(isError);
                    binding1.tvInput.setSelected(isError);
                }
            }

            binding1.tvInput.setTag(position);
            binding1.tvInput.setOnClickListener(v -> {
                ViewUtils.viewScale(v);
                int index = (int) binding1.tvInput.getTag();
                binding1.tvInput.setClickStartTime(System.currentTimeMillis());
                if (onClickSelectInterface != null) {
                    if (item.getAction().equalsIgnoreCase("DATE_PICK")) {
                        onClickSelectInterface.onClickItem(3, index, binding1.tvInput);
                    } else if (item.getAction().equalsIgnoreCase("GENDER_PICK")) {
                        onClickSelectInterface.onClickItem(5, index, binding1.tvInput);
                    } else if (item.getAction().equalsIgnoreCase("SALARY_DAY_PICK")) {
                        onClickSelectInterface.onClickItem(6, index, binding1.tvInput);
                    } else if (item.getAction().equalsIgnoreCase("BANK_PICK")) {
                        onClickSelectInterface.onClickItem(7, index, binding1.tvInput);
                    } else {
                        onClickSelectInterface.onClickItem(4, index, binding1.tvInput);
                    }
                }
                Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot());
            });

            binding1.tvTitle.setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));
            binding1.getRoot().setOnClickListener(v -> Tools.hideSoftInputFromWindow(binding1.tvTitle.getContext(), binding.getRoot()));

            if (item.getParamName().equalsIgnoreCase("salaryDay")) {
                if (TextUtils.isEmpty(salaryFrequencyValue)) {
                    binding1.getRoot().setVisibility(View.GONE);
                } else {
                    binding1.getRoot().setVisibility(View.VISIBLE);
                }
            }

        }
    }

    @Override
    protected int getItemType(SimbaInfoData item) {
        switch (item.getAction()) {
            case "INPUT":
                return 1;
            case "BANK_NUMBER_INPUT":
                return 2;
            case "SELECT":
            case "GENDER_PICK":
            case "DATE_PICK":
            case "SALARY_DAY_PICK":
            case "BANK_PICK":
                return 3;
        }
        return 0;
    }

    @Override
    protected ViewBinding createBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return SimbaPItem1Binding.inflate(inflater, parent, false);
        } else if (viewType == 2) {
            return SimbaPItem2Binding.inflate(inflater, parent, false);
        } else if (viewType == 3) {
            return SimbaPItem3Binding.inflate(inflater, parent, false);
        }
        return SimbaPItem3Binding.inflate(inflater, parent, false);
    }

    public void setStepTag(String stepTag) {
        this.stepTag = stepTag;
    }

    public void sendSexData(SimbaInfoData item, SimbaTextView textView, boolean isSet) {
        textView.setClickStartTime(System.currentTimeMillis());
        if (item != null && item.getValues() != null) {
            SimbaValueInfo man = null;
            SimbaValueInfo woman = null;
            for (SimbaValueInfo valueBean : item.getValues()) {
                if (valueBean.getValue().equalsIgnoreCase("female")) {
                    woman = valueBean;
                } else if (valueBean.getValue().equalsIgnoreCase("male")) {
                    man = valueBean;
                }
            }
            if (item.getIsMan() == 1) {
                if (man != null) {
                    item.setParamValue(man.getValue());
                    item.setParamValueTitle(man.getTitle());
                    if (!isSet) {
                        textView.setClickEndTime(System.currentTimeMillis());
                        textView.sendEvent(item.getParamValue());
                    }
                }
            } else if (woman != null) {
                item.setParamValue(woman.getValue());
                item.setParamValueTitle(woman.getTitle());
                if (!isSet) {
                    textView.setClickEndTime(System.currentTimeMillis());
                    textView.sendEvent(item.getParamValue());
                }
            }
        }
    }

    public void sendSelectData(SimbaInfoData item, SimbaTextView textView) {
        if (item != null && !TextUtils.isEmpty(item.getParamValue())) {
            textView.setClickEndTime(System.currentTimeMillis());
            textView.sendEvent(item.getParamValue());
        }
    }

    public void sendAllInput() {
        if (inputList != null && !inputList.isEmpty()) {
            for (String key : inputList.keySet()) {
                SimbaEditText editText = inputList.get(key);
                if (editText != null) {
                    editText.clearFocus();
                    String data = editText.getText().toString();
                    LogUtil.e("me", "sendAllInput data->" + data);
                    if (!TextUtils.isEmpty(data) && !editText.isSend()) {
                        editText.sendEvent(data);
                    }
                }
            }
        }
    }

    public interface ClickSelectInterface {
        void onClickItem(int type, int position, SimbaTextView textView);
    }

    public void setOnClickSelectInterface(ClickSelectInterface onClickSelectInterface) {
        this.onClickSelectInterface = onClickSelectInterface;
    }

    public String getSalaryFrequencyValue() {
        return salaryFrequencyValue;
    }

    public void setSalaryFrequencyValue(String salaryFrequencyValue) {
        this.salaryFrequencyValue = salaryFrequencyValue;
    }

    public boolean isError() {
        return isError;
    }

    public void setError(boolean error) {
        isError = error;
    }


}
